//
//  UIImageView+HKExtension.swift
//  App架构Swift
//
//  Created by 胡锦涛 on 2019/2/19.
//  Copyright © 2019 胡锦涛. All rights reserved.
//

import UIKit
import Kingfisher

// MARK: - UIImageView控件扩展
extension UIImageView{

    ///添加自旋转动画
    func hk_addRotateAni(_ duration:Float = 4) {

        if self.layer.animation(forKey: "transform.rotation.z") == nil{
            let rotationAnimation:CABasicAnimation = CABasicAnimation.init()
            rotationAnimation.toValue = NSNumber.init(value: (2 * Double.pi))
            rotationAnimation.duration = CFTimeInterval(duration)
            rotationAnimation.repeatCount = MAXFLOAT
            rotationAnimation.isRemovedOnCompletion = false
            self.layer.add(rotationAnimation, forKey: "transform.rotation.z")
        }

    }
    
    ///计算磁盘缓存
    func hk_calculateDiskImageCacheSize(_ completion: @escaping ((CGFloat) -> Void)) {
        ImageCache.default.calculateDiskCacheSize { (size) in
            completion(CGFloat(size)/1024/1024)
        }
    }
    
    ///清除磁盘缓存
    func hk_clearDiskImageCache(_ completion:@escaping (() -> Void)) {
        ImageCache.default.clearDiskCache {
            completion()
        }
    }

}

extension UIImageView {

    func hk_setImage(_ imageUrl:String?,_ placeholder: Placeholder? = nil) {
        
        guard let u = imageUrl, !u.isEmpty,let url = URL(string: u) else {
            kf.setImage(with: nil, placeholder: placeholder, options: nil, progressBlock: nil)
            return
        }
        
        kf.setImage(with: url, placeholder: placeholder, options: nil, progressBlock: nil)
        
    }
    
    func hk_setImage(_ imageUrl : String?, _ placeholder: Placeholder? = nil, completionHandler: CompletionHandler?) {
        
        guard let u = imageUrl, !u.isEmpty,let url = URL(string: u) else {
            kf.setImage(with: nil, placeholder: placeholder, options: nil, progressBlock: nil)
            return
        }
        
        kf.setImage(with: url, placeholder: placeholder, options: nil, progressBlock: nil, completionHandler: completionHandler)
        
    }
}

extension UIButton {
    
    func hk_setImage(_ imageUrl:String?,_ placeholder: Placeholder? = nil) {
        
        guard let u = imageUrl, !u.isEmpty,let url = URL(string: u) else {
            kf.setImage(with: nil, for: .normal, placeholder: placeholder as? UIImage, options: nil, progressBlock: nil)
            return
        }
        
        kf.setImage(with: url, for: .normal, placeholder: placeholder as? UIImage, options: nil, progressBlock: nil)
        
    }
    
    func hk_setImage(_ imageUrl : String?, _ placeholder: Placeholder? = nil, completionHandler: CompletionHandler?) {
        
        guard let u = imageUrl, !u.isEmpty,let url = URL(string: u) else {
            kf.setImage(with: nil, for: .normal, placeholder: placeholder as? UIImage, options: nil, progressBlock: nil)
            return
        }
        
        kf.setImage(with: url, for: .normal, placeholder: placeholder as? UIImage, options: nil, progressBlock: nil, completionHandler: completionHandler)
        
    }
    
}


